/*
 * Copyright 2007 Stéphane Brunner <stephane.brunner@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY, without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

#include <QtGui>
#include <QtOpenGL>
#include <QDebug>
#include <QImage>

#include "video.h"
#include "video.moc"

#include <iostream>
using namespace std;
#include <sys/time.h>

#define IMAGE_WIDTH 720
#define IMAGE_HEIGHT 576

#define IMAGE_RATIO 1


GLVideo::GLVideo(int width, int height, QWidget *parent) :
   QWidget(parent)
{
  setFixedSize(width, height);
  setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
  setAttribute(Qt::WA_PaintOnScreen);

  QPalette p = palette();
  p.setColor( QPalette::Window, Qt::black );
  setPalette( p );
  setBackgroundRole( QPalette::Window );
  setAutoFillBackground( true );
}

GLVideo::~GLVideo()
{
}

// 720x576 => 65ms
// rescale 2x => 20 ms
// rescale 3x => 6 ms
// rescale 4x => 5 ms
void GLVideo::setFrame(AVPicture *original_frame) {
  struct timeval start;
  gettimeofday(&start, NULL);

  QImage image((uint8_t*)resized_frame.data[0], IMAGE_WIDTH/IMAGE_RATIO, IMAGE_HEIGHT/IMAGE_RATIO, QImage::Format_ARGB32); // 0.1ms
  image = image.scaled( size(), Qt::IgnoreAspectRatio, Qt::FastTransformation );
  m_pixmap = QPixmap::fromImage( image );
  repaint();

  struct timeval end;
  gettimeofday(&end, NULL);
//  cerr<<(end.tv_sec - start.tv_sec) * 1000.0 + (end.tv_usec - start.tv_usec) / 1000.0<<endl;
}

void GLVideo::paintEvent( QPaintEvent* ev )
{
  QPainter p( this );
  p.drawPixmap( 0, 0, m_pixmap );
}
